package com.tgy.赫夫曼树.赫夫曼编码;

import netscape.javascript.JSObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/***
 * @ClassName: Code
 * @Description:
 * @Auther: tanggy3
 * @Date: 19:22 2023/3/6
 * @version : V1.0
 */
public class Code {
    public static void main(String[] args) {
        String aa = "My name is {{ name }} and I am forever {{ age }}.";
        HashMap<String, Object> stringObjectHashMap = new HashMap<String, Object>();
        stringObjectHashMap.put(" name ","Bill");
        stringObjectHashMap.put(" age ",3);
        System.out.println(getFinal(aa,stringObjectHashMap));

    }
    public static String getFinal(String content, HashMap<String,Object> value){
        ArrayList<String> list = new ArrayList<>();
        //解析content
        String pattern = "\\{\\{(.+?)\\}\\}";
        //String pattern = "\\{\\{"+"Name"+"\\}\\}";
        System.out.println(pattern);
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(content);
        int count = 0;
        while (m.find()) {

            list.add(m.group(1));
            count = m.end();
        }
        list.stream().forEach(System.out::println);
        //解析map
        if(value.size()!= list.size()){
            return "解析错误，参数数量不匹配";
        }
        for (String s : list) {
            if (!value.containsKey(s)) {
                return "解析错误，参数内容不匹配";
            }
            //content.replaceAll("\\{\\{"+s+"\\}\\}",value.get(s).toString());
            content = content.replaceAll("\\{\\{"+s+"\\}\\}",value.get(s).toString());
        }

        return content;

    }
}
